﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq;
using MyDMS.DAL;

public partial class UsersProjects : PageBaseClass
{
    MyDMSEntity myDmsEntity=new MyDMSEntity();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<Department> deptList = myDmsEntity.Department.Where(r => r.Office_Code == Author.office_Code).ToList();

            ddlDept.DataSource = deptList;
            ddlDept.DataTextField = "Department_Name";
            ddlDept.DataValueField = "Department_Code";
            ddlDept.DataBind();

            if (deptList.Count > 0)
            {
                ddlDept.SelectedIndex = 0;
                BindTheUsers(int.Parse(ddlDept.SelectedValue));
            }
            else
            {
                BindTheUsers(0);
            }
            List<Project> lstProj = myDmsEntity.Project.Where(r => r.Office_Code == Author.office_Code).ToList();
            
            ddlProjects.DataSource = lstProj;
            ddlProjects.DataTextField = "Project_Title";
            ddlProjects.DataValueField = "Project_Code";
            ddlProjects.DataBind();

            if (lstProj.Count > 0)
            {
                ddlProjects.SelectedIndex = 0;
                BindTheProjectUsers(int.Parse(ddlProjects.SelectedValue));
            }
            else
            {
                BindTheProjectUsers(0);
            }

        }
    }

    private void BindTheUsers(int deptCode)
    {
        if (deptCode >= 0)
        {
            this.ddlUsers.DataSource = myDmsEntity.User.Where(r => r.Department_Code == deptCode);
        }
        else
        {
            this.ddlUsers.DataSource = myDmsEntity.User.ToList();
        }

        this.ddlUsers.DataTextField = "LastName";// +", " + UserInfo.UserInfoFields.LastName.ToString();
        this.ddlUsers.DataTextFormatString = "Ms {0}";
        this.ddlUsers.DataValueField = "User_Code";
        this.ddlUsers.DataBind();
    }

    private void BindTheProjectUsers(int projectCode)
    {
        //object dataObject = null;
        //new SqlFactory().ExecuteQuery("MAN_Select_UsersInAProject " + projectCode, out dataObject, SqlFactory.DataObjectType.DataSet);
        if (projectCode >= 0)
        {
            this.gvUserProjects.DataSource =
                myDmsEntity.UserInProject.Where(r => r.Project_Code == projectCode).ToList();
        }
        else
        {
            this.gvUserProjects.DataSource = myDmsEntity.UserInProject.ToList();
        } 
        this.gvUserProjects.DataBind();
        //if (dataObject != null)
        //{
        //    this.gvUserProjects.DataSource = dataObject;
        //    this.gvUserProjects.DataBind();
        //}
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        UserInProject uiProj = new UserInProject();
        uiProj.Project_Code = int.Parse(ddlProjects.SelectedValue);
        uiProj.User_Code = int.Parse(ddlUsers.SelectedValue);
        myDmsEntity.UserInProject.AddObject(uiProj);
        if (myDmsEntity.SaveChanges()!=0)
        {
            lblMessage.Text = "Saved Successfully";
        }
        else
        {
            lblMessage.Text = "Error!";
        }
        lblMessage.Visible = true;
    }

    protected void ddlDept_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlDept.SelectedIndex >= 0)
        {
            BindTheUsers(int.Parse(ddlDept.SelectedValue));
        }
        else
        {
            BindTheUsers(0);
        }
    }

    protected void ddlProjects_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlProjects.SelectedIndex >= 0)
        {
            BindTheProjectUsers(int.Parse(ddlProjects.SelectedValue));
        }
        else
        {
            BindTheProjectUsers(0);
        }
    }
}
